home *** CD-ROM | disk | FTP | other *** search
- /*
- File: LinkedList.cp
-
- Contains: A lame linked list class
-
- Written by: Kent Miller
-
- Copyright: © 1995 Apple Computer
-
- Notes: When I started writing this sample code, there wasn't a
- LinkedList class in Sprocket. In Dave's January 96 update
- to Sprocket, he included a better linked list class but I'm
- too lazy to revise this program use his class.
-
- Change History (most recent first):
-
- */
-
- #include "LinkedList.h"
-
- TLinkedList::TLinkedList()
- {
- this->head = nil;
- }
-
- TLinkedList::~TLinkedList()
- {
- if (this->head != nil)
- {
- while (this->head != nil)
- this->RemoveFromList(this->head->elem);
-
- }
- }
-
- void *
- TLinkedList::RemoveFromList (void * obj)
- {
- ListElem *trailer = this->head;
- ListElem *theList = this->head;
- void * elem;
-
- while ((theList != nil) && (theList->elem != obj))
- {
- trailer = theList;
- theList = theList->next;
- }
-
- if (theList == nil)
- return nil;
-
- elem = theList->elem;
- if (theList == this->head)
- {
- this->head = this->head->next;
- DisposePtr ( (Ptr) theList);
- }
- else
- {
- trailer->next = theList->next;
- DisposePtr ( (Ptr) theList);
- }
-
- return elem;
- }
-
-
- void
- TLinkedList::AddToList (void * obj)
- {
- ListElem *theList;
-
- theList = (ListElem *) NewPtr (sizeof (ListElem));
- theList->elem = obj;
- theList->next = this->head;
- this->head = theList;
- }
-
- void *
- TLinkedList::GetFirstListElem()
- {
- if (this->head)
- return this->head->elem;
- else
- return nil;
- }
-
- SInt16
- TLinkedList::CountListItems()
- {
- ListElem * theList = this->head;
- SInt16 count = 0;
-
- while (theList)
- {
- count++;
- theList = theList->next;
- }
-
- return count;
- }
-
- void *
- TLinkedList::GetNextListElem(void * obj)
- {
- ListElem * theList = this->head;
-
- while ((theList != nil) && (theList->elem != obj))
- theList = theList->next;
-
- if (theList == nil)
- return nil;
- else if (theList->next == nil)
- return nil;
- else
- return theList->next->elem;
-
- }
-
-